<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        img {
            /* width: 50%; */
            height: 50%;
        }

        .table tr td {
            vertical-align: middle
        }
    </style>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">
</head>

<body>
    <div id="app">
        <h1>{{name}}</h1>
        <table class='table table-striped table-bordered text-center text-justify'>
            <thead>
                <tr>
                    <!-- th  td 兄弟 -->
                    <th>全选<input type="checkbox" v-model="checkAll"></th>
                    <th>商品</th>
                    <th></th>
                    <th></th>
                    <th>单价</th>
                    <th>数量</th>
                    <th>小计</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(item,index) in lists">
                    <!-- 我们要循环展示 tr -->
                    <td><input v-model="item.isSelect" type="checkbox"></td>
                    <td><img :src="item.pic" alt="图片"></td>
                    <td>{{item.title}}</td>
                    <td>{{item.desc}}</td>
                    <td>{{item.price|money}}</td>
                    <!-- 商品的属量 -->
                    <td><input type="number" min=0 v-model='item.count'></td>
                    <!-- 商品的总价 -->

                    <td>{{item.price*item.count|money}}</td>
                    <td><button type="button" class="btn btn-danger">删除</button>
                    </td>
                </tr>
                <tr>
                    <td colspan='8'>
                        <button type="button" class="btn btn-danger">清空购物车</button>
                    </td>
                </tr>
                <tr>
                    <td colspan="8">
                        总价：{{total|money}}
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</body>

</html>
<script src="../../node_modules/vue/dist/vue.js"></script>
<script>

    const vm = new Vue({
        el: "#app",
        data() {
            return {
                name: 'xxsd',
                lists: [],
            }
        },
        filters: {
            money: function (value) {
                return (value / 100).toFixed(2) + '$';
            }
        },
        watch: {

        },
        computed: {
            checkAll: {
                get() {
                    return this.lists.every(item => item.isSelect)
                },
                set(val) {
                    this.lists.forEach(item => {
                        item.isSelect = val
                    });
                }
            },
            total: {
                get() {
                   return this.lists.reduce((prev, cur) => {
                        if (cur.isSelect) {
                            return prev + cur.count * cur.price
                        } else {
                            return prev
                        }
                    },0)
                }
            }
        },
        created() {
            this.getData();
        },
        mounted() {

        },
        methods: {
            getData() {
                // 用来获取数据的
                fetch('./data.json').then(res => res.json()).then(data => {
                    console.log(data)
                    this.lists = data;
                })
            },

        }
    })

</script>
<!-- <script src="./index.js"></script> -->